package com.ljph.ljaker.app;

import com.jfinal.config.*;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.ljph.jfinal.base.BaseController;
import com.ljph.jfinal.json.EnhancedFastJsonFactory;
import com.ljph.ljaker.controller.CommonController;
import com.ljph.ljaker.controller.GoodController;
import com.ljph.ljaker.controller.UserController;
import com.ljph.ljaker.interceptor.CookieInterceptor;
import com.ljph.ljaker.interceptor.LoginInterceptor;
import com.ljph.ljaker.model.Good;
import com.ljph.ljaker.model._MappingKit;
import com.ljph.ljaker.model.ext.ExtBrand;
import com.ljph.ljaker.model.ext.ExtGood;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.security.provider.MD5;

import java.util.Date;

/**
 * JFinal 应用总配置
 * Created by yuzhou on 16/9/2.
 */
public class AppConfig extends JFinalConfig {

    private static final Logger _log = LoggerFactory.getLogger(AppConfig.class);
    private static AppConfig me = null;

    public static AppConfig me() {
        return me;
    }

    public static final String SESSION_USER = "session_user";
    public static final String COOKIE_USER = "ljaker_user_id";
    public static final int COOKIE_DURATION = 60 * 60 * 24 * 7; // 登录COOKIE保留时间，单位秒

    public static final String SUPER = "superadmin"; // 系统内置超级用户的用户名
    public static final String SALT = "rnfo093kdtnc,mnvqo4939jfkdjfkhhvjcvmcvoqonf"; // 系统内置超级用户的密码混淆值
    public static final String DS = "mysql";
    public static final String DS_PR = "mysql-pr";

    public AppConfig() {
        _log.info("App Config will be constructed");
        me = this;
    }

    @Override
    public void configConstant(Constants constants) {
        loadPropertyFile("init.properties");
        constants.setDevMode(getPropertyToBoolean("devMode", true));

        constants.setViewType(ViewType.FREE_MARKER);
        constants.setBaseViewPath("/view/ftl");
        constants.setFreeMarkerViewExtension("ftl");
        constants.setJsonFactory(EnhancedFastJsonFactory.me());
    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/", CommonController.class);

        // JSON APIs
        routes.add("/api/user", UserController.class);
        routes.add("/api/good", GoodController.class);
    }

    @Override
    public void configPlugin(Plugins plugins) {

        Boolean showSql = getPropertyToBoolean("showSql", true);
        // 引入本系统数据库
        Prop prop = PropKit.use("config/" + DS + ".properties");
        C3p0Plugin ds = new C3p0Plugin(prop.getProperties());
        plugins.add(ds);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(DS, ds);
        arp.setShowSql(showSql);
        _MappingKit.mapping(arp);
        plugins.add(arp);

        // 引入外部产品数据库
        Prop propExt = PropKit.use("config/" + DS_PR + ".properties");
        C3p0Plugin dsPr = new C3p0Plugin(propExt.getProperties());
        plugins.add(dsPr);

        ActiveRecordPlugin arpPr = new ActiveRecordPlugin(DS_PR, dsPr);
        arpPr.setShowSql(showSql);

        arpPr.addMapping(ExtBrand.TABLE_NAME, ExtBrand.PRIMARY_KEY, ExtBrand.class);
        arpPr.addMapping(ExtGood.TABLE_NAME, ExtGood.PRIMARY_KEY, ExtGood.class);
        plugins.add(arpPr);

        // 引入 ehcache 缓存插件
        plugins.add(new EhCachePlugin());
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new CookieInterceptor());
        interceptors.add(new LoginInterceptor());
    }

    @Override
    public void configHandler(Handlers handlers) {
        handlers.add(new ContextPathHandler("ctx"));
    }

    @Override
    public void afterJFinalStart() {
        BaseController.ERROR_KEY = "error";
        _log.info("JFinal started successfully ...");
    }

    @Override
    public void beforeJFinalStop() {
        _log.info("JFinal will be stopped ...");
    }

    public static void main(String[] args) {
        //System.out.println(DigestUtils.md5Hex(DigestUtils.md5Hex(DigestUtils.md5Hex("*IK<9ol.0p;/")) + SALT));
        System.out.println(new Date().getTime());
        System.out.println((int)(new Date().getTime()));

    }
}